package com.zhiyou100.taxi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.taxi.dao.CarOutMapper;
import com.zhiyou100.taxi.entity.CarOut;
import com.zhiyou100.taxi.entity.CarOutExample;
import com.zhiyou100.taxi.service.CarInService;
import com.zhiyou100.taxi.service.CarOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-24 19:26
 * @Description
 */
@Transactional
@Service
public class CarOUtServiceImpl implements CarOutService {
    @Autowired
    CarOutMapper carOutMapper;

    //根据 客户id 参数 汽车 car_out
    @Override
    public int deleteByCustomerId(String customerId) {
        CarOutExample example = new CarOutExample();
        CarOutExample.Criteria criteria = example.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        return carOutMapper.deleteByExample(example);
    }

    @Override
    public int deleteByCarId(String crId) {
        CarOutExample example = new CarOutExample();
        CarOutExample.Criteria criteria = example.createCriteria();
        criteria.andCarIdEqualTo(crId);
        return carOutMapper.deleteByExample(example);

    }

    @Override
    public PageInfo<CarOut> queryPage(Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<CarOut> list = getAll();
        PageInfo<CarOut> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<CarOut> getAll() {
        return carOutMapper.selectByExample(null);
    }

    @Override
    public CarOut getByPrimaryKey(String carOutId) {
        CarOut carOut = carOutMapper.selectByPrimaryKey(carOutId);
        return carOut;
    }

    @Override
    public int insert(CarOut carOut) {
        return carOutMapper.insert(carOut);

    }

    @Override
    public int updateByPrimaryKey(CarOut carOut) {
        return carOutMapper.updateByPrimaryKeySelective(carOut);

    }

    @Autowired
    CarInService carInService;

    @Override
    public int deleteByPrimaryKey(String carOutId) {
        carInService.deleteByCarOutId(carOutId);//删除外键
        return carOutMapper.deleteByPrimaryKey(carOutId);
    }

    @Override
    public String getPrimaryKey() {
        Integer key = carOutMapper.selectPrimaryKey();
        return "car_out_" + key;
    }

    @Override
    public int deleteByPrimaryKeyS(Set<String> set) {
        try {
            set.forEach((c) -> {
                carInService.deleteByCarOutId(c);//删除外键
                carOutMapper.deleteByPrimaryKey(c);
            });
            return set.size();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }
}
